package com.sxj.myleetcode.march;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: myleetcode
 * @Package: com.sxj.myleetcode.march
 * @ClassName: Main
 * @Author: sxj
 * @Description:
 * @Date: 2024/3/21 15:09
 * @Version: 1.0.0
 */
public class Main0321 {

    public static void main(String[] args) {
//        test02();
//        ListNode a1 = new ListNode(1);
//        ListNode a2 = new ListNode(3);
//        ListNode a3 = new ListNode(3);
//        a1.next = a2;
//        a2.next = a3;
//        ListNode node = deleteDuplicates(null);
//        while (node != null){
//            System.out.println(node.val);
//            node = node.next;
//        }

        test03();
    }

    /**
     * 合并2个数组，不要创建新数组
     * @param nums1  数组的长度是 m + n
     * @param m 第一个数组有数据的个数
     * @param nums2  数组的长度为n
     * @param n 第一个数组有数据的个数
     */
    public static void mergeArr(int[] nums1, int m, int[] nums2, int n){
        int cur = 0;
        for (int i = 0; i < n; i++) {
            int toCompare = nums2[i]; //待比较的
            for (int j = 0; j < m; j++) {
            }
        }
    }

    public static void test03(){
        //给你两个二进制字符串 a 和 b ，以二进制字符串的形式返回它们的和。
        // 某位：（a + b + 进位）%2    进位： （a + b + 进位）/2
        String a = "1010";
        String b = "1011";
        char[] aChar = a.toCharArray();
        char[] bChar = b.toCharArray();
        //1 需要反着计算
        int i = aChar.length - 1,j = bChar.length - 1;
        int carry = 0;
        StringBuffer sb = new StringBuffer();
        while (i >= 0 || j >= 0){
            int sum = carry;
            sum += (i >= 0? aChar[i] - '0' : 0);
            sum += (j >= 0? bChar[j] - '0' : 0);
            sb.append(sum%2);
            carry = sum / 2;
            i--;
            j--;
        }
        //2 添加上进位
        sb.append(carry);

        //记得求反
        sb.reverse();
        System.out.println(sb.toString());

    }

    /**
     * TODO 
     * @param head
     */
    public static ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head;
        while (cur != null &&  cur.next != null){
            if (cur.val == cur.next.val){
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }
        return head;


    }

    /**
     * 集合去重
     *
     */
    public static void test02(){
        //1 list集合如何去重重复数据
        List<Integer> list = new ArrayList<>(Arrays.asList(1,1,3));
        int cur = 0;
        while (cur < list.size() -1 ){
            if (list.get(cur).equals(list.get(cur + 1))){
                list.remove(list.get(cur));
            }else {
                cur ++;
            }
        }
        list.forEach(System.out::println);
    }



    /**
     * 集合去重
     *
     */
    public static void test(){
        //1 list集合如何去重重复数据
        List<Integer> list = Arrays.asList(1, 1, 3);
        ArrayList<Integer> newLst = new ArrayList<>(list.size());
        for (int i = 0; i < list.size(); i++) {
            if (!newLst.contains(list.get(i))){
                newLst.add(list.get(i));
            }
        }
        newLst.forEach(System.out::println);
    }


}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}